package com.hymjweb.demo.hymj.log_manage.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.log_manage.bean.XtBackupLogMsgPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author DaTing created on 2020-09-17
 **/
@Mapper
@Component
public interface XtBackupLogMsgMapper {

    class XtBackupLogMsgProvider{
        public String insert(XtBackupLogMsgPojo obj) {
            String sql= new SQL() {{
                INSERT_INTO("xt_backup_log_msg");
                VALUES("id", "#{id}");
                VALUES("table_name", "#{tableName}");
                VALUES("table_id", "#{tableId}");
                VALUES("pojo_name", "#{pojoName}");
                VALUES("oper_title", "#{operTitle}");
                VALUES("oper_memo", "#{operMemo}");
                VALUES("backup_log_msg", "#{backupLogMsg}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("create_user_name", "#{createUserName}");
                VALUES("create_time_short", "#{createTimeShort}");
            }}.toString();
            return sql;
        }

        public String findAll(Map map) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("xt_backup_log_msg t");
                WHERE("1=1");
            }}.toString();

            String keyCreateUserId = DataTypeUtil.toStringObject(map.get("keyCreateUserId"));//记录添加人
            String keyStartDate = DataTypeUtil.toStringObject(map.get("keyStartDate"));//记录创建时间起
            String keyEndDate = DataTypeUtil.toStringObject(map.get("keyEndDate"));//记录创建时间止

            if(keyCreateUserId!=null){
                sql += " and t.create_user_id = " + DataTypeUtil.quoteStr(keyCreateUserId);
            }


            if(keyStartDate!=null){
                if(keyEndDate!=null){
                    int d=keyStartDate.compareTo(keyEndDate);
                    if(d>0){
                        String tempDate = keyStartDate;
                        keyStartDate = keyEndDate;
                        keyEndDate = tempDate;
                    }
                    sql += " and (date_format(t.create_time,'%Y-%m-%d') between "+ DataTypeUtil.quoteStr(keyStartDate) +" and " + DataTypeUtil.quoteStr(keyEndDate) + ")";
                }
                else{
                    sql += " and date_format(t.create_time,'%Y-%m-%d') >= "+ DataTypeUtil.quoteStr(keyStartDate);
                }
            }
            else{
                if(keyEndDate!=null){
                    sql += " and date_format(t.create_time,'%Y-%m-%d') <= "+ DataTypeUtil.quoteStr(keyEndDate);
                }
            }

            return sql;

        }
    }

    @InsertProvider(type = XtBackupLogMsgProvider.class, method = "insert")
    void insert(XtBackupLogMsgPojo pojo);

    @Results(id = "XtBackupLogMsgMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "table_name", property = "tableName"),
            @Result(column = "table_id", property = "tableId"),
            @Result(column = "oper_title", property = "operTitle"),
            @Result(column = "oper_memo", property = "operMemo"),
            @Result(column = "pojo_name", property = "pojoName"),
            @Result(column = "backup_log_msg", property = "backupLogMsg",jdbcType=JdbcType.BLOB),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "create_user_name", property = "createUserName"),
            @Result(column = "create_time_short", property = "createTimeShort")
    })
    @SelectProvider(type = XtBackupLogMsgProvider.class, method = "findAll")
    List<XtBackupLogMsgPojo> findAll(Map map);

}
